********************************************************************************	
//Instruments 
cd $pathdata_processed_cc
use comb22005, clear

foreach year of numlist 2006/2009 {
	append using comb2`year'
	}

merge 1:1 npid fagkode using tirage_temp, nogen

gen chance = (predicted_gpa-expected_gpa)
gen chance_sd = (predicted_gpa-expected_gpa)/(variance_gpa)
replace chance_sd = chance if variance_gpa == 0

//grades
bysort npid: egen exam_grades = mean(real_skr)
bysort npid: egen oral_grades = mean(real_mun)

bysort npid: egen teachers_grades = mean(real_stp)
bysort npid: egen sd_teachers_grades = sd(real_stp)
replace sd_teachers_grades = 0 if nb_kurs == 1

bysort npid: egen mean_skr_hat = mean(skr_hat)
bysort npid: egen sd_skr_hat = sd(skr_hat)
replace sd_skr_hat = 0 if nb_kurs == 1

//kurs & grades
foreach i of numlist 1/1 {
	gen stp`i' = real_stp == `i'
	bysort npid: egen nb_course_`i' = sum(stp`i')
	}

keeporder npid nb_kurs nb_exam nb_oral teachers_grades sd_teachers_grades mean_skr_hat sd_skr_hat Tirage_final Fagkode expected_gpa predicted_gpa variance_gpa chance chance_sd exam_grades oral_grades nb_course_1 
duplicates drop

save chance_cc, replace
